﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ATEGo
{
    //———————————————————————【指令的变量和特殊指令】————————————————————————
    public partial class Executor
    {

        /// <summary>
        /// 将命令中的变量替换成正常的值
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public string ReplaceCmdParas(string cmd, bool isReg = false)
        {
            //替换命令里面的变量：命令里面定义的。
            foreach (var item in Params_InCmd)
            {
                cmd = cmd.Replace(item.Key, isReg ? item.Value.ToReg() : item.Value);
            }
            //替换命令里面的抓取正则值。形如$0
            foreach (var item in Params_GrabResult)
            {
                cmd = cmd.Replace(item.Key, isReg ? item.Value.ToReg() : item.Value);
            }
            return cmd;
        }
        
        /// <summary>
        /// 命令是不是注释了
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static bool CmdIsAnn(string cmd)
        {
            if (cmd.StartsWith("###"))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 是不是由C#执行的特权指令
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static bool CmdIsSpecial(string cmd)
        {
            if (cmd.StartsWith("#") && cmd.EndsWith("#"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 是否要求此条语句pass
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public static bool CmdIsPass(string cmd)
        {
            if (cmd == "#PASS#")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        

        /// <summary>
        /// 执行特权指令，类似 #....#
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        private async Task<bool> ExecuteSpecialCmd(string cmd, CmdDict cmdDict)
        {
            //暂停
            //
            if (cmd.StartsWith("#PAUSE#"))
            {
                int time = cmd.Replace("PAUSE", "").Replace("#", "").Trim().TimeToMS();
                WriteFlow_Content($"暂停{time}毫秒");
                await Task.Delay(time);
            }
            //结束本次执行，重新打开cmd
            else if (cmd.StartsWith("#RESTART#"))
            {
                WriteFlow_Content($"重启cmd程序");
                WindowsCmd_Open();
            }
            else if (cmd.StartsWith("#SEND_ONCE#"))
            {
                WindowsCmd_SendOnce();
            }
            else if (cmd.StartsWith("#SEND_TWICE#"))
            {
                WindowsCmd_SendTwice();
            }
            //"cmd":"#SET_VALUE#",
            //"params":"$$has_startup$$=true,$$num$$=1"
            else if (cmd.StartsWith("#SET_VALUE#"))
            {
                var arr = cmdDict["params"].Split(',');
                foreach (var item in arr)
                {
                    var s = item.Split('=');
                    Params_InCmd[s[0].Trim()] = s[1].Trim();
                }
            }
            //"cmd":"#SHOW_INPUT#",
            //"params":"请输入单板条码：,$$input_board_barcode$$,(\\d){12}"
            else if (cmd.StartsWith("#SHOW_INPUT#"))
            {
                var arr = cmdDict["params"].Split(',');

                Input input = new Input();
                input.Reminder = arr[0].Trim();
                if (arr.Length > 2) 
                {
                    input.CheckReg = arr[2].Trim();
                }
                
                input.ShowDialog(This_Form);
                Params_InCmd[arr[1].Trim()] = input.UserInput;
                WriteCmd(arr[1].Trim()+" = "+ input.UserInput);
            }
            
            return true;
        }

        

    }
}
